package com.ksy.common.view;

import android.content.Context;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.ksy.common.R;


/**
 * Created by Ksy
 */

public class HeadBar extends LinearLayout {

    private Context context;
    private TextView tv_title;
    private BackClickListener backClickListener;
    private LinearLayout ll_leftContainer;
    private LinearLayout ll_centerContainer;
    private LinearLayout ll_rightContainer;
    private ImageView iv_right;
    private TextView tv_right;
    private ImageView iv_left;
    private DisplayMetrics displayMetrics;
    private final LinearLayout headBar_root;
    private int colorResId = R.color.common_colorTextDark;
    private ImageView iv_titleLeft;
    private ImageView iv_titleRight;
    private int titleRightResId;
    private int titleLeftResId;

    public HeadBar(Context context) {
        this(context, null);
    }

    public HeadBar(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
        headBar_root = (LinearLayout) LayoutInflater.from(context).inflate(R.layout.common_layout_headbar, this, true);
        ll_leftContainer = findViewById(R.id.ll_leftContainer);
        ll_centerContainer = findViewById(R.id.ll_centerContainer);
        ll_rightContainer = findViewById(R.id.ll_rightContainer);
    }

    public HeadBar setTitle(String title) {
        return setTitle(title, false);
    }

    public HeadBar setTitle(String title, boolean isLeft) {
        ll_leftContainer.setVisibility(VISIBLE);
        ll_rightContainer.setVisibility(VISIBLE);
        ll_centerContainer.removeAllViews();
        if (tv_title == null) {
            tv_title = generateTitleTextView(isLeft);
            setTv_titleColor(colorResId);
        }
        RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) ll_centerContainer.getLayoutParams();
        layoutParams.leftMargin = dp2Px(55);
        layoutParams.rightMargin = dp2Px(55);
        tv_title.setText(title);
        ll_centerContainer.addView(tv_title);
        ViewGroup.LayoutParams params = getLayoutParams();
        if (params == null) {
            params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        }
        params.width = ViewGroup.LayoutParams.MATCH_PARENT;
        params.height = ViewGroup.LayoutParams.WRAP_CONTENT;
        headBar_root.setLayoutParams(params);
        return this;
    }

    public HeadBar setTitle(String title, int lImage, int rImage) {
        ll_leftContainer.setVisibility(VISIBLE);
        ll_rightContainer.setVisibility(VISIBLE);
        ll_centerContainer.removeAllViews();
        if (tv_title == null) {
            tv_title = generateTitleTextView(false);
            setTv_titleColor(colorResId);
        }
        if (lImage > 0) {
            if (iv_titleLeft == null) {
                iv_titleLeft = generateTitleImageView();
            }
            setTitleLeftImage(lImage);
            ll_centerContainer.addView(iv_titleLeft);
        }

        RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) ll_centerContainer.getLayoutParams();
        layoutParams.leftMargin = dp2Px(55);
        layoutParams.rightMargin = dp2Px(55);
        ll_centerContainer.setLayoutParams(layoutParams);

        tv_title.setText(title);

        ll_centerContainer.addView(tv_title);
        if (rImage > 0) {
            if (iv_titleRight == null) {
                iv_titleRight = generateTitleImageView();
            }
            setTitleRightImage(rImage);
            ll_centerContainer.addView(iv_titleRight);
        }

        ViewGroup.LayoutParams params = getLayoutParams();
        if (params == null) {
            params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        }
        params.width = ViewGroup.LayoutParams.MATCH_PARENT;
        params.height = ViewGroup.LayoutParams.WRAP_CONTENT;
        headBar_root.setLayoutParams(params);
        return this;
    }

    private TextView generateTitleTextView(boolean isLeft) {
        TextView textView = new TextView(context);
        LayoutParams params = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, dp2Px(40));
        textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 18);
        textView.setGravity(isLeft ? Gravity.CENTER_VERTICAL | Gravity.LEFT : Gravity.CENTER);
        textView.setSingleLine(true);
        textView.setEllipsize(TextUtils.TruncateAt.END);
        textView.setLayoutParams(params);
        return textView;
    }

    private TextView generateRightTextView() {
        TextView textView = new TextView(context);
        LayoutParams params = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        params.rightMargin = dp2Px(12);
        textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 16);
        textView.setGravity(Gravity.CENTER);
        textView.setSingleLine(true);
        if (colorResId >= 0) {
            textView.setTextColor(getResources().getColor(colorResId));
        }
        textView.setEllipsize(TextUtils.TruncateAt.END);
        textView.setLayoutParams(params);
        return textView;
    }

    public HeadBar setTv_titleColor(int colorResId) {
        this.colorResId = colorResId;
        if (tv_title != null) {
            tv_title.setTextColor(getResources().getColorStateList(colorResId));
        }
        return this;
    }

    public HeadBar setTitleLeftImage(int titleLeftResId) {
        this.titleLeftResId = titleLeftResId;
        if (iv_titleLeft != null) {
            iv_titleLeft.setBackgroundResource(titleLeftResId);
        }
        return this;
    }

    public HeadBar setTitleRightImage(int titleRightResId) {
        this.titleRightResId = titleRightResId;
        if (iv_titleRight != null) {
            iv_titleRight.setBackgroundResource(titleRightResId);
        }
        return this;
    }

    public HeadBar setRightText(String text, OnClickListener clickListener) {
        ll_rightContainer.removeAllViews();
        if (tv_right == null) {
            tv_right = generateRightTextView();
            tv_right.setTextColor(getResources().getColorStateList(R.color.common_colorWhite));
        }
        tv_right.setText(text);
        tv_right.setOnClickListener(clickListener);
        ll_rightContainer.addView(tv_right);
        ll_rightContainer.setVisibility(VISIBLE);
        return this;
    }

    public HeadBar setRightTextBg(int resId) {
        if (tv_right != null) {
            tv_right.setPadding(dp2Px(8), dp2Px(3), dp2Px(8), dp2Px(3));
            tv_right.setBackgroundResource(resId);
        }

        return this;
    }

    public HeadBar setRightTextColor(int color) {
        if (tv_right != null) {
            tv_right.setTextColor(getResources().getColorStateList(color));
        }
        return this;
    }

    public HeadBar setTitleText(String titleText) {
        if (tv_title != null) {
            tv_title.setText(titleText);
        }
        return this;
    }

    public HeadBar setTitleClickListener(View.OnClickListener clickListener) {
        if (tv_title != null) {
            tv_title.setOnClickListener(clickListener);
        }
        return this;
    }

    public View setTitle(int layoutId) {
        ViewGroup.LayoutParams params = getLayoutParams();
        if (params == null) {
            params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        }
        if (layoutId <= 0) {
            params.width = ViewGroup.LayoutParams.MATCH_PARENT;
            params.height = 0;
            headBar_root.setLayoutParams(params);
            return null;
        }
        params.width = ViewGroup.LayoutParams.MATCH_PARENT;
        params.height = ViewGroup.LayoutParams.WRAP_CONTENT;
        headBar_root.setLayoutParams(params);
        ll_centerContainer.removeAllViews();
        View view = LayoutInflater.from(context).inflate(layoutId, ll_centerContainer, false);
        RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) ll_centerContainer.getLayoutParams();
        ll_leftContainer.setVisibility(GONE);
        ll_rightContainer.setVisibility(GONE);
        layoutParams.leftMargin = 0;
        layoutParams.rightMargin = 0;
        ll_centerContainer.setLayoutParams(layoutParams);
        ll_centerContainer.addView(view);
        return view;
    }

    public View setTitle(int layoutId, boolean isFull) {
        ViewGroup.LayoutParams params = getLayoutParams();
        if (params == null) {
            params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        }
        if (layoutId <= 0) {
            params.width = ViewGroup.LayoutParams.MATCH_PARENT;
            params.height = 0;
            headBar_root.setLayoutParams(params);
            return null;
        }
        params.width = ViewGroup.LayoutParams.MATCH_PARENT;
        params.height = ViewGroup.LayoutParams.WRAP_CONTENT;
        headBar_root.setLayoutParams(params);
        ll_centerContainer.removeAllViews();
        View view = LayoutInflater.from(context).inflate(layoutId, ll_centerContainer, false);
        if (isFull) {
            RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) ll_centerContainer.getLayoutParams();
            ll_leftContainer.setVisibility(GONE);
            ll_rightContainer.setVisibility(GONE);
            layoutParams.leftMargin = 0;
            layoutParams.rightMargin = 0;
            ll_centerContainer.setLayoutParams(layoutParams);
        }
        ll_centerContainer.addView(view);
        return view;
    }

    private int dp2Px(int dp) {
        if (displayMetrics == null) {
            displayMetrics = context.getResources().getDisplayMetrics();
        }
        return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, displayMetrics);
    }

    public HeadBar setRightIcon(int resID, OnClickListener clickListener) {
        ll_rightContainer.removeAllViews();
        if (iv_right == null) {
            iv_right = generateLeftOrRightImageView();
        }
        iv_right.setImageResource(resID);
        iv_right.setOnClickListener(clickListener);
        ll_rightContainer.addView(iv_right);
        ll_rightContainer.setVisibility(VISIBLE);
        return this;
    }

    public HeadBar clearRight() {
        ll_rightContainer.removeAllViews();
        return this;
    }

    public HeadBar setLeftIcon(int resID, OnClickListener clickListener) {
        setLeftIcon(resID);
        iv_left.setOnClickListener(clickListener);
        return this;
    }

    public HeadBar setLeftIcon(int resID) {
        ll_leftContainer.removeAllViews();
        if (iv_left == null) {
            iv_left = generateLeftOrRightImageView();
        }
        iv_left.setImageResource(resID);
        ll_leftContainer.addView(iv_left);
        ll_leftContainer.setVisibility(VISIBLE);
        return this;
    }

    public HeadBar setHeadBarHeight(int dp) {
        ViewGroup.LayoutParams params = getLayoutParams();
        if (params == null) {
            params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        }
        params.width = ViewGroup.LayoutParams.MATCH_PARENT;
        params.height = dp2Px(dp);
        headBar_root.setLayoutParams(params);
        return this;
    }

    public HeadBar setBackClickListener(BackClickListener backClickListener) {
        this.backClickListener = backClickListener;
        return this;
    }


    public HeadBar setIv_LeftVisible(boolean isShowLeftButton) {
        if (iv_left != null) {
            iv_left.setVisibility(isShowLeftButton ? VISIBLE : GONE);
        }
        return this;
    }

    public LinearLayout getLl_leftContainer() {
        return ll_leftContainer;
    }

    public LinearLayout getCenterContainer() {
        return ll_centerContainer;
    }

    public LinearLayout getLl_rightContainer() {
        return ll_rightContainer;
    }

    public LinearLayout getRootContainer() {
        return headBar_root;
    }

    public interface BackClickListener {
        void onBackClick();
    }

    private ImageView generateLeftOrRightImageView() {
        LayoutParams params = new LayoutParams(dp2Px(45), dp2Px(40));
        params.gravity = Gravity.CENTER_VERTICAL;
        ImageView icon = new ImageView(getContext());
//        int margin = dp2Px(15);
//        if (isLeft) {
//            icon.setPadding(margin, 0, margin, 0);
//        } else {
//            icon.setPadding(0, 0, margin, 0);
//        }
        icon.setLayoutParams(params);
        icon.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
        return icon;
    }

    private ImageView generateTitleImageView() {
        LayoutParams params = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        params.gravity = Gravity.CENTER_VERTICAL;
        params.setMargins(dp2Px(5), dp2Px(5), dp2Px(5), dp2Px(5));
        ImageView icon = new ImageView(getContext());
        icon.setMaxHeight(dp2Px(40));
        icon.setMaxWidth(dp2Px(80));
//        int margin = dp2Px(15);
//        if (isLeft) {
//            icon.setPadding(margin, 0, margin, 0);
//        } else {
//            icon.setPadding(0, 0, margin, 0);
//        }
        icon.setLayoutParams(params);
        icon.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
        return icon;
    }

    @Override
    public void setBackgroundColor(int color) {
        super.setBackgroundColor(color);
    }
}
